$(function () {
	//var urlRequest = GetRequest();
	var postId = postId || getUrlParam('postId') || getUrlParam('postid');
	var postType = postType || getUrlParam('postType');
	var pageNum = 1;
	//var isAdmin = 




	//评论用户弹出框事件
	$('.tie-comments').on('click', '.comment-cust', function () {
		var custName = $(this).attr('data-custName');
		var repliedCustNo = $(this).attr('data-custNo');
		$(this).addClass('clicked');
		$('.mask-layer,.dialog-box').fadeIn(function () {
			$('.dialog-box .reply').attr('data-custName', custName);
			$('.dialog-box .reply').attr('repliedCustNo', repliedCustNo);
		});

	})
	//评论用户取消
	$('.dialog-box').on('click', '.cancle', function () {
		$('.mask-layer,.dialog-box').fadeOut();
		$('.comment-cust').removeClass('clicked');
	})
	//评论用户
	$('.dialog-box').on('click', '.reply', function () {

		$('.mask-layer,.dialog-box').fadeOut();
		$('.comment-cust').removeClass('clicked');
		var custName = $(this).attr("data-custName");
		var repliedCustNo = $(this).attr('repliedcustno');
		var pathname = location.pathname;
		location.href = '/duimen/detail/comment.html?postId=' + postId + '&repliedCustNo=' + repliedCustNo + '&custName=' + custName + '&postType=' + postType + '&pathname=' + pathname;;
	})
	//复制评论
	$('.dialog-box').on('click', '.copy', function (e) {
		// $('.mask-layer,.dialog-box').fadeOut();
		// $('.comment-cust').removeClass('clicked');
		// Detail.copyText($('.comment-cust .what'));
		//alert('待定')
		//alert('待定')
		var clipboard = new Clipboard('.copy', {
			text: function (trigger) {
				console.log(trigger);
				return $('.clicked').find('.ctt').attr('data-clipboard-text')
			}
		});
		clipboard.on('success', function (e) {
			AlertBox({
				type: 'mini',
				msg: '复制成功'
			})
			$('.mask-layer,.dialog-box').fadeOut();
			$('.comment-cust').removeClass('clicked');

		});
		//clipboard.destroy();

	})
	//举报评论
	$('.dialog-box').on('click', '.report', function () {
		$('.mask-layer,.dialog-box').fadeOut();
		$('.comment-cust').removeClass('clicked');
		var relationId = $('.dialog-box .reply').attr('repliedcustno');
		$('.report-box').fadeIn().attr('relationId', relationId);
	})
	//评论用户弹出框事件end
	//滚动到底部刷新
	var commentLockon = true;
	window.onscroll = function (e) {
		if (document.body.clientHeight - window.screen.height - window.scrollY < 10 && commentLockon) {
			commentLockon = false;
			Detail.getCommentMore(postId, ++pageNum);
		}
	}
})
var Detail = {
	getCommentMore: function (postId, pageNum) {
		$.ajax({
			type: 'GET',
			url: baseurl + '/post/query/commentsMore.do',
			dataType: 'jsonp',
			data: {
				postId: postId,
				pageNum: pageNum
			}
		}).then(function (data) {
			if (!data.resultCode) {
				console.log(data);
				for(var i =0,l=data.result.length;i<l;i++){
					//var date = new Date(data.result[i].commentTime);
					data.result[i].commentTime = getDateDiff(data.result[i].commentTime);

				}
				var html = template('tpl_commentsMore', data);
				$('.tie-comments>ul').append($(html));
				commentLockon = true;
			} else {
				console.log(data.resultMsg)
			}
		})
	},
	getPraise: function (result) {
		var praiseHtml = template('tpl_praiseList', result)
		$('.tie-like').html(praiseHtml);
		console.log(praiseHtml);

	},

	getComment:function(result){
		for(var i =0,l=result.commentList.length;i<l;i++){
			//var date = new Date(result.commentList[i].commentTime);
			result.commentList[i].commentTime = getDateDiff(result.commentList[i].commentTime);
		}
		var html = template('tpl_commentsList', result);
		$('.tie-comments>ul').html(html);
		$('#commentsSum').html('所有评论(' + result.commentCount + ')');

	},
	//点赞
	clickPraise: function (postId, type) {
		checkLogin(function () {
			$.ajax({
				url: baseurl + '/post/save/praise.do',
				type: 'GET',
				dataType: 'jsonp',
				data: {
					postId: postId,
					type: type
				}
			}).then(function (data) {
				if (data.resultCode == 0) {
					console.log(data.resultMsg);
					var postId = postId || getUrlParam('postId');
					var postType = postType || getUrlParam('postType');
					Detail.freshPraise(postId, postType);
				} else {
					console.log(data.resultMsg);
				}
			})
		})

	},
	//获取菜单
	freshPraise: function (postId, postType) {
		$.ajax({
			type: 'GET',
			url: baseurl + '/post/query/info.do',
			dataType: 'jsonp',
			data: {
				postId: postId,
				postType: postType
			}
		}).then(function (data) {
			if (data.resultCode == 0) {
				console.log(data.result);

				Detail.getPraise(data.result);
				console.log('fresh');

			} else {
				console.log(data.resultMsg)
			}
		})
	},
	getMenuList: function (postId, postType, isAdmin, checkNo) {
		var obj = { 'admin': isAdmin, 'checkNo': checkNo };
		var html = template('tpl_menuBox', obj);
		$('.post-coment .menu').html(html);
		commentBarFun(postId,postType,isAdmin);//底部评论点赞及菜单事件绑定
	},
	//删除帖子
	deletePostComment: function (relationId, relationType) {
		$.ajax({
			url: '/post/delete.do',
			url: baseurl + '/post/delete.do',
			type: 'GET',
			dataType: 'jsonp',
			data: {
				'relationId': relationId,
				'relationType': relationType
			}
		}).then(function (data) {
			if (data.resultCode == 0) {
				console.log('已删除');
				location.href = "/duimen"
			}
		})
	},
	//提出举报
	submitReport: function (isAdmin, relationId, relationType, reportType) {
		checkLogin(function () {
			$.ajax({
				url: baseurl + '/post/report.do',
				type: 'GET',
				dataType: 'jsonp',
				data: {
					'relationId': relationId,
					'relationType': relationType,
					'reportType': reportType
				}
			}).then(function (data) {
				if (data.resultCode == 0) {
					console.log('举报成功');
					AlertBox({ type: 'mini', alertCls: 'alertBox', msg: '感谢您的举报，我们会尽快处理～' });
					$('.report-box').fadeOut();
					if (relationType == 0) {//有问题
						$('.menu-box .report').html('<i></i>已举报').addClass('reported');
						if ($('.menu-box')[0].style.width == '') {
							$('.menu-box').css({ 'width': '1.8rem', 'left': '-.78rem' })
						}
					}
				} else {
					console, log(data.resultMsg);
				}
			})
		})
	},
	//复制内容??????
	copyText: function (obj) {
		var rng = document.body.createTextRange();
		rng.moveToElementText(obj[0]);
		//rng.scrollIntoView();
		rng.select();
		rng.execCommand("Copy");
		rng.collapse(false);
		AlertBox({ type: 'mini', msg: '已复制到剪切剪贴板' });
	},
	getCommPraise: function (data) {
		var postId = postId || getUrlParam('postId') || getUrlParam('postid');
		var postType = postType || getUrlParam('postType');
		Detail.getComment(data);//第一次调用获取评论区
		Detail.getPraise(data);//第一次调用获取点赞区
		if (data.praiseFlag) {
			$('.praise').addClass('praised');
		}
		Detail.checkCustNo(postId, postType, data.custNo);
		//评论帖子
		$('.postit').on('touchend', function (e) {
			location.href = '/duimen/detail/comment.html?postId=' + postId + '&postType=' + postType
		})
	},
	checkCustNo: function (postId, postType, custNo) {
		var checkNo = false;
		var id = Util.getCookie('custno');
		if(!id){
			$('.post-coment').click(function(){
				checkLogin();
			})
		}
    	if(id==custNo){
    		checkNo = true;
    	}
    	console.log(id);
    	Detail.checkAdmin(postId,postType,checkNo);//是否是管理员
	},
	checkAdmin: function (postId, postType, checkNo) {
		$.ajax({
			url: baseurl + '/user/adminInfo.do',
			type: 'GET',
			dataType: 'jsonp'
		}).then(function (data) {
			if (data.resultCode == 0) {
				console.log('是否是管理员');
				console.log(data);
				console.log(checkNo);
				Detail.getMenuList(postId, postType, data.result.admin, checkNo);

			}else{

				console.log(data.resultMsg);
			}
		})
	}
}
//时间对象格式化
Date.prototype.Format = function (fmt) {
	var o = {
		"M+": this.getMonth() + 1, //月份 
		"d+": this.getDate(), //日 
		"h+": this.getHours(), //小时 
		"m+": this.getMinutes(), //分 
		"s+": this.getSeconds(), //秒 
		"q+": Math.floor((this.getMonth() + 3) / 3), //季度 
		"S": this.getMilliseconds() //毫秒 
	};
	if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
	for (var k in o)
		if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
	return fmt;
}
var commentBarFun = function (postId, postType, isAdmin) {
	//点赞或者取消赞
	$('.post-coment .praise').click(function () {
		if ($(this).hasClass('praised')) {
			$(this).removeClass('praised');
			var type = 1;
		} else {
			$(this).addClass('praised');
			var type = 0;
		}
		console.log(type);
		Detail.clickPraise(postId, type);
	});
	//菜单弹出框
	$('.post-coment .menu').click(function () {
		$('.menu-box').fadeToggle();
	})
	//置顶弹出框
	$('.post-coment').on('click', '.set-top', function () {
		var that = this;
		AlertBox({
			type: 'mini', msg: '已置顶', alertCls: 'alertBox', callback: function () {
				$(that).html('<i></i>已置顶');

				if ($('.menu-box')[0].style.width == '') {
					$('.menu-box').css({ 'width': '1.8rem', 'left': '-.78rem' })
				}
			}
		});

	})
	//举报弹出框
	$('.post-coment').on('click', '.report', function () {
		if (!$(this).hasClass('reported')) {
			$('.report-box').fadeIn().removeAttr('relationid');
		}
	})
	$('.report-box').on('click', ':not(.cancle)', function () {
		var relationType = $('.report-box').attr('relationId') ? 1 : 0;
		var relationId = $('.report-box').attr('relationId') ? $('.report-box').attr('relationId') : postId;
		var reportType = $(this).index();
		console.log(reportType);
		Detail.submitReport(isAdmin, relationId, relationType, reportType);
		$('.report-box').removeAttr('relationId');
	})
	$('.report-box').on('click', '.cancle', function () {
		$('.report-box').fadeOut();
		$('.report-box').removeAttr('relationId');
	})
	//审核弹出框
	$('.post-coment').on('click', '.review', function () {
		var that = this;
		AlertBox({
			type: 'mini', msg: '审核不通过', alertCls: 'alertBox', callback: function () {
				$(that).html('<i></i>审核不通过');
				$('.menu-box').css({ 'width': '2.3rem', 'left': '-1.2rem' });
				$('.menu-box .icon').css('left', '60%');
			}
		});

	})
	//删除弹出框
	$('.post-coment').on('click', '.delete', function () {
		var that = this;
		AlertBox({
			type: 'doubleBtn',
			msg: '是否确定删除本条帖子？',
			alertCls: 'alertBox',
			confirm: function () {
				Detail.deletePostComment(postId, 0)//关联id??? 关联类型0：帖子 1：评论
			}
		});

	})
}